#include <dbg.h>

#include <cassert>

using namespace std;

class Solution {
public:
    int maximumWhiteTiles(vector<vector<int>>& tiles, int carpetLen) {
        sort(tiles.begin(), tiles.end());
        int res = 0;
        int left = 0;
        int covered = 0;
        for (auto& tile : tiles) {
            int start = tile[0];  // 滑窗右节点
            int end = tile[1];
            covered += end - start + 1;  // 记录当前已经覆盖的瓷砖数量
            while (end - tiles[left][1] + 1 >
                   carpetLen) {  // 如果当前瓷砖的长度大于地毯的长度
                covered -=
                    tiles[left][1] - tiles[left][0] + 1;  // 滑窗左节点右移
                ++left;
            }
            int uncovered = max(0,
                                end - tiles[left][0] - carpetLen +
                                    1);  // 记录当前未覆盖的瓷砖数量
            res = max(res, covered - uncovered);
        }
        return res;
    }
};

int main() {
    Solution s;

    vector<vector<int>> tiles1{{1, 5}, {10, 11}, {12, 18}, {20, 25}, {30, 32}};
    vector<vector<int>> tiles2{{10, 11}, {1, 1}};

    int carpetLen1 = 10;
    int carpetLen2 = 2;

    assert(s.maximumWhiteTiles(tiles1, carpetLen1) == 9);
    assert(s.maximumWhiteTiles(tiles2, carpetLen2) == 2);

    return 0;
}